<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\common\service\qrcode;

use app\common\model\store\shop\Clerk as clerkUserModel;
use Exception;
use Grafika\Color;
use Grafika\Grafika;

/**
 * 内部分享海报
 * Class Qrcode
 * @package app\common\service
 */
class PosterNeibu extends BaseQRcode {


	// 分销商海报设置
	private $channel;

	/**
	 * 构造方法
	 * Poster constructor.
	 * @param clerkUserModel $clerk
	 * @param string $channel 二维码渠道(小程序码、h5码)
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function __construct($order, string $channel = 'MP-WEIXIN') {

		parent::__construct();
		
		$this->order = $order;
	
		// 二维码渠道
		$this->channel = $channel;

	}

	/**
	 * 获取分销二维码
	 * @return string
	 * @throws \app\common\exception\BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws Exception
	 */
	public function getImage(): string {
		// 如果已生成过，则直接返回url
		if (file_exists($this->getPosterPath())) {
			//return $this->getPosterUrl();
		}

		// 海报背景图
		$backdrop = __DIR__ . '/resource/neibu.png';


		// 4. 拼接海报图
		return $this->savePoster($backdrop);
	}

	/**
	 * 获取用户头像
	 * @return string
	 */
	private function userAvatar(): string {
		$avatar = $this->clerk['user']['avatar'];
		//$default = __DIR__ . '/resource/default-avatar.png';
		$default = base_url() . 'temp/default-avatar.png';
		return !empty($avatar) ? $avatar['external_url'] : $default;
	}

	/**
	 * 海报图文件路径
	 * @return string
	 */
	private function getPosterPath(): string {
		// 保存路径
		$tempPath = web_path() . "temp/share-neibu/".Date('Ymd').'/';
		!is_dir($tempPath) && mkdir($tempPath, 0755, true);
		return $tempPath . $this->getPosterName();
	}

	/**
	 * 海报图文件名称
	 * @return string
	 */
	private function getPosterName(): string {
		return "poster_{$this->order['id']}" . '.png';
	}

	/**
	 * 海报图url
	 * @return string
	 */
	private function getPosterUrl(): string {
		return base_url() . "temp/share-neibu/".Date('Ymd').'/' . $this->getPosterName() . '?t=' . time();
	}

	/**
	 * 拼接海报图
	 * @param string $backdrop 背景图路径
	 * @param string $avatar 头像路径
	 * @param string $qrcode 二维码路径
	 * @return string
	 * @throws Exception
	 */
	private function savePoster(string $backdrop): string {
		// 实例化图像编辑器
		$editor = Grafika::createEditor(['Gd']);
		// 打开海报背景图
		$editor->open($backdropImage, $backdrop);


		// 写入日期
		$fontSize = 16;
		$fontX = 100;
		$fontY = 230;
		$Color = new Color('#cc2222');
		$fontPath = Grafika::fontsDir() . '/st-heiti-light.ttc';
		$editor->text($backdropImage, '预定时间：'.$this->order['date_label'].$this->order['time'], $fontSize, $fontX, $fontY, $Color, $fontPath);

		// 写入姓名
		$fontSize = 16;
		$fontX = 100;
		$fontY = 270;
		$Color = new Color('#cc2222');
		$fontPath = Grafika::fontsDir() . '/st-heiti-light.ttc';
		$editor->text($backdropImage, '预定贵宾：'.$this->order['real_name'], $fontSize, $fontX, $fontY, $Color, $fontPath);

		// 写入人数
		$fontSize = 16;
		$fontX = 100;
		$fontY = 310;
		$Color = new Color('#cc2222');
		$fontPath = Grafika::fontsDir() . '/st-heiti-light.ttc';
		$editor->text($backdropImage, '预定人数：'.$this->order['to_people'].'人', $fontSize, $fontX, $fontY, $Color, $fontPath);

		// 保存图片
		$editor->save($backdropImage, $this->getPosterPath());
		return $this->getPosterUrl();
	}

	/**
	 * 生成圆形图片
	 * @param string $imgpath 图片地址
	 * @param string $saveName 保存文件名，默认空。
	 */
	private function circular(string $imgpath, string $saveName = '') {
		$srcImg = imagecreatefromstring(file_get_contents($imgpath));
		$w = imagesx($srcImg);
		$h = imagesy($srcImg);
		$w = $h = min($w, $h);
		$newImg = imagecreatetruecolor($w, $h);
		// 这一句一定要有
		imagesavealpha($newImg, true);
		// 拾取一个完全透明的颜色,最后一个参数127为全透明
		$bg = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
		imagefill($newImg, 0, 0, $bg);
		$r = $w / 2; //圆半径
		for ($x = 0; $x < $w; $x++) {
			for ($y = 0; $y < $h; $y++) {
				$rgbColor = imagecolorat($srcImg, $x, $y);
				if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
					imagesetpixel($newImg, $x, $y, $rgbColor);
				}
			}
		}
		// 输出图片到文件
		imagepng($newImg, $saveName);
		// 释放空间
		imagedestroy($srcImg);
		imagedestroy($newImg);
	}
}